home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / frasrc19.zip / SLIDESHW.C < prev    next >
Text File  |  1995-01-16  |  9KB  |  379 lines

  1. /***********************************************************************/
  2. /* These routines are called by getakey to allow keystrokes to control */
  3. /* Fractint to be read from a file.                       */
  4. /***********************************************************************/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <ctype.h>
  9. #include <time.h>
  10. #include <string.h>
  11. #ifndef XFRACT
  12. #include <conio.h>
  13. #endif
  14. #include "fractint.h"
  15. #include "prototyp.h"
  16.  
  17. static void sleep_secs(int);
  18. static int  showtempmsg_txt(int,int,int,int,char *);
  19. static void message(int secs, char far *buf);
  20. static void slideshowerr(char far *msg);
  21. static int  get_scancode(char *mn);
  22. static char far *get_mnemonic(int code);
  23.  
  24. struct scancodes
  25. {
  26.    int code;
  27.    char far *mnemonic;
  28. };
  29. static struct scancodes far scancodes[] =
  30. {
  31.    {  ENTER,      "ENTER"       },
  32.    {  INSERT,      "INSERT"      },
  33.    {  DELETE,      "DELETE"      },
  34.    {  ESC,      "ESC"         },
  35.    {  TAB,      "TAB"         },
  36.    {  PAGE_UP,      "PAGEUP"      },
  37.    {  PAGE_DOWN,  "PAGEDOWN"    },
  38.    {  HOME,      "HOME"        },
  39.    {  END,      "END"         },
  40.    {  LEFT_ARROW, "LEFT"        },
  41.    {  RIGHT_ARROW,"RIGHT"       },
  42.    {  UP_ARROW,   "UP"          },
  43.    {  DOWN_ARROW, "DOWN"        },
  44.    {  F1,      "F1"          },
  45.    {  -1,      NULL        }
  46. };
  47. #define stop sizeof(scancodes)/sizeof(struct scancodes)-1
  48.  
  49. static int get_scancode(char *mn)
  50. {
  51.    int i;
  52.    i = 0;
  53.    for(i=0;i< stop;i++)
  54.       if(far_strcmp((char far *)mn,scancodes[i].mnemonic)==0)
  55.      break;
  56.    return(scancodes[i].code);
  57. }
  58.  
  59. static char far *get_mnemonic(int code)
  60. {
  61.    int i;
  62.    i = 0;
  63.    for(i=0;i< stop;i++)
  64.       if(code == scancodes[i].code)
  65.      break;
  66.    return(scancodes[i].mnemonic);
  67. }
  68. #undef stop
  69.  
  70. char busy = 0;
  71. static FILE *fpss = NULL;
  72. static long starttick;
  73. static long ticks;
  74. static int slowcount;
  75. static unsigned int quotes;
  76. static char calcwait = 0;
  77. static int repeats = 0;
  78. static int last1 = 0;
  79. static FCODE smsg[] = "MESSAGE";
  80. static FCODE sgoto[] = "GOTO";
  81. static FCODE scalcwait[] = "CALCWAIT";
  82. static FCODE swait[] = "WAIT";
  83.  
  84. /* places a temporary message on the screen in text mode */
  85. static int showtempmsg_txt(int row, int col, int attr,int secs,char *txt)
  86. {
  87.    int savescrn[80];
  88.    int i;
  89.    if(text_type > 1)
  90.       return(1);
  91.    for(i=0;i<80;i++)
  92.    {
  93.       movecursor(row,i);
  94.       savescrn[i] = get_a_char();
  95.    }
  96.    putstring(row,col,attr,txt);
  97.    movecursor(25,80);
  98.    sleep_secs(secs);
  99.    for(i=0;i<80;i++)
  100.    {
  101.       movecursor(row,i);
  102.       put_a_char(savescrn[i]);
  103.    }
  104.    return(0);
  105. }
  106.  
  107. static void message(int secs, char far *buf)
  108. {
  109.    int i;
  110.    char nearbuf[41];
  111.    i = -1;
  112.    while(buf[++i] && i< 40)
  113.       nearbuf[i] = buf[i];
  114.    nearbuf[i] = 0;
  115.    if(text_type < 2)
  116.       showtempmsg_txt(0,0,7,secs,nearbuf);
  117.    else if (showtempmsg(nearbuf) == 0)
  118.       {
  119.      sleep_secs(secs);
  120.      cleartempmsg();
  121.       }
  122. }
  123.  
  124. /* this routine reads the file autoname and returns keystrokes */
  125. int slideshw()
  126. {
  127.    int out,err,i;
  128.    char buffer[81];
  129.    if(calcwait)
  130.    {
  131.       if(calc_status == 1 || busy) /* restart timer - process not done */
  132.      return(0); /* wait for calc to finish before reading more keystrokes */
  133.       calcwait = 0;
  134.    }
  135.    if(fpss==NULL)   /* open files first time through */
  136.       if(startslideshow()==0)
  137.      {
  138.      stopslideshow();
  139.      return (0);
  140.      }
  141.  
  142.    if(ticks) /* if waiting, see if waited long enough */
  143.    {
  144.       if(clock_ticks() - starttick < ticks) /* haven't waited long enough */
  145.      return(0);
  146.       ticks = 0;
  147.    }
  148.    if (++slowcount <= 18)
  149.    {
  150.       starttick = clock_ticks();
  151.       ticks = CLK_TCK/5; /* a slight delay so keystrokes are visible */
  152.       if (slowcount > 10)
  153.      ticks /= 2;
  154.    }
  155.    if(repeats>0)
  156.    {
  157.       repeats--;
  158.       return(last1);
  159.    }
  160. start:
  161.    if(quotes) /* reading a quoted string */
  162.    {
  163.       if((out=fgetc(fpss)) != '\"' && out != EOF)
  164.      return(last1 = out);
  165.       quotes = 0;
  166.    }
  167.    /* skip white space: */
  168.    while ((out=fgetc(fpss)) == ' ' || out == '\t' || out == '\n') { }
  169.    switch(out)
  170.    {
  171.       case EOF:
  172.      stopslideshow();
  173.      return(0);
  174.       case '\"':        /* begin quoted string */
  175.      quotes = 1;
  176.      goto start;
  177.       case ';':         /* comment from here to end of line, skip it */
  178.      while((out=fgetc(fpss)) != '\n' && out != EOF) { }
  179.      goto start;
  180.       case '*':
  181.      if (fscanf(fpss,"%d",&repeats) != 1
  182.        || repeats <= 1 || repeats >= 256 || feof(fpss))
  183.      {
  184.         static FCODE msg[] = "error in * argument";
  185.         slideshowerr(msg);
  186.         last1 = repeats = 0;
  187.      }
  188.      repeats -= 2;
  189.      return(out = last1);
  190.    }
  191.  
  192.    i = 0;
  193.    for(;;) /* get a token */
  194.    {
  195.       if(i < 80)
  196.      buffer[i++] = (char)out;
  197.       if((out=fgetc(fpss)) == ' ' || out == '\t' || out == '\n' || out == EOF)
  198.      break;
  199.    }
  200.    buffer[i] = 0;
  201.    if(buffer[i-1] == ':')
  202.       goto start;
  203.    out = -12345;
  204.    if(isdigit(buffer[0]))    /* an arbitrary scan code number - use it */
  205.      out=atoi(buffer);
  206.    else if(far_strcmp((char far *)buffer,smsg)==0)
  207.       {
  208.      int secs;
  209.      out = 0;
  210.      if (fscanf(fpss,"%d",&secs) != 1)
  211.      {
  212.         static FCODE msg[] = "MESSAGE needs argument";
  213.         slideshowerr(msg);
  214.      }
  215.      else
  216.      {
  217.         int len;
  218.         char buf[41];
  219.         buf[40] = 0;
  220.         fgets(buf,40,fpss);
  221.         len = strlen(buf);
  222.         buf[len-1]=0; /* zap newline */
  223.         message(secs,(char far *)buf);
  224.      }
  225.      out = 0;
  226.       }
  227.    else if(far_strcmp((char far *)buffer,sgoto)==0)
  228.       {
  229.      if (fscanf(fpss,"%s",buffer) != 1)
  230.      {
  231.         static FCODE msg[] = "GOTO needs target";
  232.         slideshowerr(msg);
  233.         out = 0;
  234.      }
  235.      else
  236.      {
  237.         char buffer1[80];
  238.         rewind(fpss);
  239.         strcat(buffer,":");
  240.         do
  241.         {
  242.            err = fscanf(fpss,"%s",buffer1);
  243.         } while( err == 1 && strcmp(buffer1,buffer) != 0);
  244.         if(feof(fpss))
  245.         {
  246.            static FCODE msg[] = "GOTO target not found";
  247.            slideshowerr(msg);
  248.            return(0);
  249.         }
  250.         goto start;
  251.      }
  252.       }
  253.    else if((i = get_scancode(buffer)) > 0)
  254.      out = i;
  255.    else if(far_strcmp(swait,(char far *)buffer)==0)
  256.       {
  257.      float fticks;
  258.      err = fscanf(fpss,"%f",&fticks); /* how many ticks to wait */
  259.      fticks *= CLK_TCK;        /* convert from seconds to ticks */
  260.      if(err==1)
  261.      {
  262.         ticks = (long)fticks;
  263.         starttick = clock_ticks();    /* start timing */
  264.      }
  265.      else
  266.      {
  267.         static FCODE msg[] = "WAIT needs argument";
  268.         slideshowerr(msg);
  269.      }
  270.      slowcount = out = 0;
  271.       }
  272.    else if(far_strcmp(scalcwait,(char far *)buffer)==0) /* wait for calc to finish */
  273.       {
  274.      calcwait = 1;
  275.      slowcount = out = 0;
  276.       }
  277.    else if((i=check_vidmode_keyname(buffer)) != 0)
  278.       out = i;
  279.    if(out == -12345)
  280.    {
  281.       char msg[80];
  282.       sprintf(msg,s_cantunderstand,buffer);
  283.       slideshowerr(msg);
  284.       out = 0;
  285.    }
  286.    return(last1 = out);
  287. }
  288.  
  289. startslideshow()
  290. {
  291.    if((fpss=fopen(autoname,"r"))==NULL)
  292.       slides = 0;
  293.    ticks = 0;
  294.    quotes = 0;
  295.    calcwait = 0;
  296.    slowcount = 0;
  297.    return(slides);
  298. }
  299.  
  300. void stopslideshow()
  301. {
  302.    if(fpss)
  303.       fclose(fpss);
  304.    fpss = NULL;
  305.    slides = 0;
  306. }
  307.  
  308. void recordshw(int key)
  309. {
  310.    char far *mn;
  311.    float dt;
  312.    dt = (float)ticks;       /* save time of last call */
  313.    ticks=clock_ticks();  /* current time */
  314.    if(fpss==NULL)
  315.       if((fpss=fopen(autoname,"w"))==NULL)
  316.      return;
  317.    dt = ticks-dt;
  318.    dt /= CLK_TCK;  /* dt now in seconds */
  319.    if(dt > .5) /* don't bother with less than half a second */
  320.    {
  321.       if(quotes) /* close quotes first */
  322.       {
  323.      quotes=0;
  324.      fprintf(fpss,"\"\n");
  325.       }
  326.       fprintf(fpss,"WAIT %4.1f\n",dt);
  327.    }
  328.    if(key >= 32 && key < 128)
  329.    {
  330.       if(!quotes)
  331.       {
  332.      quotes=1;
  333.      fputc('\"',fpss);
  334.       }
  335.       fputc(key,fpss);
  336.    }
  337.    else
  338.    {
  339.       if(quotes) /* not an ASCII character - turn off quotes */
  340.       {
  341.      fprintf(fpss,"\"\n");
  342.      quotes=0;
  343.       }
  344.       if((mn=get_mnemonic(key)) != NULL)
  345. #ifndef XFRACT
  346.       fprintf(fpss,"%Fs",mn);
  347. #else
  348.           fprintf(fpss,"%s",mn);
  349. #endif
  350.       else if (check_vidmode_key(0,key) >= 0)
  351.      {
  352.         char buf[10];
  353.         vidmode_keyname(key,buf);
  354.         fprintf(fpss,buf);
  355.      }
  356.       else /* not ASCII and not FN key */
  357.      fprintf(fpss,"%4d",key);
  358.       fputc('\n',fpss);
  359.    }
  360. }
  361.  
  362. /* suspend process # of seconds */
  363. static void sleep_secs(int secs)
  364. {
  365.    long stop;
  366.    stop = clock_ticks() + (long)secs*CLK_TCK;
  367.    while(clock_ticks() < stop && kbhit() == 0) { } /* bailout if key hit */
  368. }
  369.  
  370. static void slideshowerr(char far *msg)
  371. {
  372.    char msgbuf[300];
  373.    static FCODE errhdg[] = "Slideshow error:\n";
  374.    stopslideshow();
  375.    far_strcpy(msgbuf,errhdg);
  376.    far_strcat(msgbuf,msg);
  377.    stopmsg(0,msgbuf);
  378. }
  379.